package com.coders.activities;

import org.cocos2d.nodes.CCDirector;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.coders.R;

public class EnterHighscore extends Dialog {
	String TOTALSCORE = "score";
	String PLAYERNAME = "playername";

	SharedPreferences sharedPreference = CCDirector.sharedDirector()
			.getActivity().getSharedPreferences("sharedPreference", 0);
	TextView Highscore;

	Button save;
	long highscore;
	String player_name;
	EditText playername;

	public EnterHighscore(Context context, long higscore) {

		this(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
		this.highscore = higscore;
		player_name = playername.getText().toString();

		Highscore.setText("New Highscore : " + String.valueOf(highscore));

	}

	public EnterHighscore(Context context, int theme) {
		super(context, theme);

		LinearLayout scrollView = new LinearLayout(context);

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.enterhigscore, null);
		layout.setPadding(20, 20, 20, 20);
		scrollView.addView(layout, new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		this.setContentView(scrollView);
		initViewComponents();

		setListenersForViews();

	}

	private void initViewComponents() {
		Highscore = (TextView) findViewById(R.id.textView1);

		save = (Button) findViewById(R.id.button1);
		playername = (EditText) findViewById(R.id.editText1);
	}

	private void setListenersForViews() {

		save.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				player_name = playername.getText().toString();
				SharedPreferences.Editor Editor = sharedPreference.edit();
				Editor.putString(TOTALSCORE, String.valueOf(highscore));
				Editor.putString(PLAYERNAME, player_name);
				Editor.commit();
				dismiss();
			}
		});

	}

}
